package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.LimitConfirmItemErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.LimitConfirmItemDO;
import com.cmc.cloud.cmclink.doc.mapper.LimitConfirmItemMapper;
import com.cmc.cloud.cmclink.doc.service.LimitConfirmItemService;
import com.cmc.cloud.cmclink.doc.vo.limitconfirmitemvo.LimitConfirmItemBaseVO;
import com.cmc.cloud.cmclink.doc.vo.limitconfirmitemvo.LimitConfirmItemCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.limitconfirmitemvo.LimitConfirmItemPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.limitconfirmitemvo.LimitConfirmItemUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * 信息确认类明细 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class LimitConfirmItemServiceImpl implements LimitConfirmItemService {

    @Resource
    private LimitConfirmItemMapper limitConfirmItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createLimitConfirmItem(LimitConfirmItemCreateReqVO createReqVO) {
        // 插入
        LimitConfirmItemDO limitConfirmItem = BeanUtil.copyProperties(createReqVO, LimitConfirmItemDO.class);
        limitConfirmItemMapper.insert(limitConfirmItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLimitConfirmItem(LimitConfirmItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateLimitConfirmItemExists(updateReqVO.getId());
        // 更新
        LimitConfirmItemDO updateObj = BeanUtil.copyProperties(updateReqVO, LimitConfirmItemDO.class);
        limitConfirmItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteLimitConfirmItem(Long id) {
        // 校验存在
        validateLimitConfirmItemExists(id);
        // 删除
        limitConfirmItemMapper.deleteById(id);
    }

    private void validateLimitConfirmItemExists(Long id) {
        if (limitConfirmItemMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(LimitConfirmItemErrorCodeConstants.LIMIT_CONFIRM_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public LimitConfirmItemDO getLimitConfirmItem(Long id) {
        return limitConfirmItemMapper.selectById(id);
    }

    @Override
    public List<LimitConfirmItemDO> getLimitConfirmItemByMainId(Long mainId) {
        List<LimitConfirmItemDO> limitConfirmItemDos = limitConfirmItemMapper.selectList(new LambdaQueryWrapperX<LimitConfirmItemDO>().eqIfPresent(LimitConfirmItemDO::getMainId, mainId));
        return limitConfirmItemDos;
    }

    @Override
    public List<LimitConfirmItemDO> getLimitConfirmItemList(Collection<Long> ids) {
        return limitConfirmItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LimitConfirmItemDO> getLimitConfirmItemPage(LimitConfirmItemPageReqVO reqVO) {
        LambdaQueryWrapperX<LimitConfirmItemDO> queryWrapperX = new LambdaQueryWrapperX<>();
        return limitConfirmItemMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public List<LimitConfirmItemBaseVO> getLimitConfirmItemByMainIds(List<Long> mainIds) {
        List<LimitConfirmItemBaseVO> confirmList = new ArrayList<>();
        if (CollectionUtil.isEmpty(mainIds)) {
            return confirmList;
        }
        return BeanUtil.copyToList(limitConfirmItemMapper.selectList(new LambdaQueryWrapperX<LimitConfirmItemDO>().in(LimitConfirmItemDO::getMainId, mainIds)), LimitConfirmItemBaseVO.class);
    }
}
